/* 
 *  Copyright 2009-2010 Michal Trzcinka
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software distributed
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the
 *  specific language governing permissions and limitations under the License.
 */
YAHOO.namespace('TRZCINKA.VCARD.Link');

(function() {

  var Config = YAHOO.TRZCINKA.VCARD.Config,
  Dom = YAHOO.util.Dom,
  Constants = YAHOO.TRZCINKA.VCARD.Constants,
  Lang = YAHOO.util.Lang,
  LOGGER = new YAHOO.widget.LogWriter('link_painter.js');

  var HEIGHT = Config.CELL_HEIGHT,
  WIDTH = Config.CELL_WIDTH,
  PADDING = Config.ITEM_PADDING;

  YAHOO.TRZCINKA.VCARD.Link = function(from, to, wrapper) {
    this.from = from;
    this.to = to;
    this._wrapper = wrapper;
    this._htmlElements = [];

    if (to.row == from.row) {
      this._drawHorizontalLine();
    } else {
      this._drawNormalLine();
    }
  };

  YAHOO.TRZCINKA.VCARD.Link.prototype._drawHorizontalLine = function() {
    var goLeft = this.to.column < this.from.column;
    var startXY = {
      x: goLeft ? (WIDTH * this.from.column + PADDING) : (WIDTH * (this.from.column + 1) - PADDING),
      y: HEIGHT * (this.from.row + 0.5)
    }
    var step = (goLeft ? 'L' : 'R') + ':' + (WIDTH * (Math.abs(this.from.column - this.to.column) - 1) + 2 * PADDING);
    this._drawLine(startXY, [step], Config.LINK_WIDTH);
  };

  YAHOO.TRZCINKA.VCARD.Link.prototype._drawNormalLine = function() {
    var goUp = this.to.row < this.from.row;
    var goLeft = this.to.column < this.from.column;

    var steps = [];
    var startXY = {
      x: WIDTH * this.from.column + WIDTH * 0.5,
      y: goUp ? (HEIGHT * this.from.row + PADDING) : ((HEIGHT * (this.from.row + 1) - PADDING))
    }

    var verticalDirection = (goUp ? 'U' : 'D');
    var horizontalDirection = (goLeft ? 'L' : 'R');

    steps.push(verticalDirection + ':' + PADDING);
    var rowDifference = Math.abs(this.from.row - this.to.row);
    if (rowDifference > 0) {
      steps.push(verticalDirection + ':' + (rowDifference  - 1) * HEIGHT);
    }
    var columnDifference = Math.abs(this.from.column - this.to.column);
    if (columnDifference > 0) {
      steps.push(horizontalDirection + ':' + (columnDifference * WIDTH));
    }
    steps.push(verticalDirection + ':' + PADDING);

    this._drawLine(startXY, steps, Config.LINK_WIDTH);
  }

  YAHOO.TRZCINKA.VCARD.Link.prototype.destroy = function() {
    var that = this;
    this._htmlElements.each(function(div) {
      that._wrapper.removeChild(div);
    });
  }

  //expects: [D:50, U:123 L:44]
  YAHOO.TRZCINKA.VCARD.Link.prototype._drawLine = function(startXY, steps, width) {
    var result = [];
    var that = this;
    steps.each(function (step) {
      var stepElements = step.split(':');
      var direction = stepElements[0];
      var length = parseInt(stepElements[1]);
      LOGGER.log('Drawing line; startXY: ' + Lang.dump(startXY) + ', dir: ' + direction + ', length: ' + length);
      var rect;
      if (direction == 'U') {
        rect = {
          x: startXY.x,
          y: startXY.y - length,
          width: width,
          height: length
        }
        startXY.y -= length;
      } else if (direction == 'D') {
        rect = {
          x: startXY.x,
          y: startXY.y,
          width: width,
          height: length
        }
        startXY.y += length;
      } else if (direction == 'L') {
        rect = {
          x: startXY.x - length,
          y: startXY.y,
          width: length + width,
          height: width
        }
        startXY.x -= length;
      } else if (direction == 'R') {
        rect = {
          x: startXY.x,
          y: startXY.y,
          width: length + width,
          height: width
        }
        startXY.x += length;
      }
      result.push(that._drawRect(rect));
    })
    return result;
  }

  YAHOO.TRZCINKA.VCARD.Link.prototype._drawRect = function(rect) {
    LOGGER.log('Drawing rect: ' + Lang.dump(rect));
    var div = document.createElement('div');
    Dom.setStyle(div, 'width', rect.width + 'px');
    Dom.setStyle(div, 'height', rect.height + 'px');
    Dom.setStyle(div, 'backgroundColor', Config.LINK_COLOR);
    Dom.setStyle(div, 'position', 'absolute');
    Dom.setStyle(div, 'left', rect.x + 'px');
    Dom.setStyle(div, 'top', rect.y + 'px');
    this._wrapper.appendChild(div);
    this._htmlElements.push(div);
    return div;
  }

})();
